/**
 * TagSpaces - universal file and folder organizer
 * Copyright (C) 2017-present TagSpaces UG (haftungsbeschraenkt)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License (version 3) as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

import { app, Menu, nativeImage, Tray } from 'electron';

let tray = null;

const icon1x =
  '';
const icon2x =
  '';

export default function buildTrayIconMenu(mainPageProps: any, i18n, isMacLike) {
  const cKey = isMacLike ? ' -  Cmd' : ' - Ctrl';

  function openNextFile() {
    mainPageProps.openNextFile();
  }

  function openPrevFile() {
    mainPageProps.openPrevFile();
  }

  function playResumePlayback() {
    mainPageProps.resumePlayback();
  }

  function quitApp() {
    app.quit();
  }

  const trayMenuTemplate = [
    {
      label: i18n.t('newWindow'),
      click: () => mainPageProps.createNewWindowInstance()
    },
    {
      type: 'separator'
    },
    {
      label: i18n.t('showTagSpaces') + cKey + '+Shift+W',
      click: mainPageProps.showTagSpaces
    },
    {
      label: i18n.t('showSearch') + cKey + '+Shift+F',
      click: mainPageProps.openSearch
    },
    {
      type: 'separator'
    },
    {
      label: i18n.t('newFileNote') + cKey + '+Shift+N',
      click: mainPageProps.toggleCreateFileDialog
    },
    {
      type: 'separator'
    },
    {
      label: i18n.t('openNextFileTooltip') + cKey + '+Shift+D',
      click: openNextFile
    },
    {
      label: i18n.t('openPrevFileTooltip') + cKey + '+Shift+A',
      click: openPrevFile
    },
    {
      type: 'separator'
    },
    {
      label: i18n.t('pauseResumePlayback') + cKey + '+Shift+P',
      click: playResumePlayback
    },
    {
      type: 'separator'
    },
    {
      label: i18n.t('quitTagSpaces') + cKey + '+Q',
      click: quitApp
    }
  ];

  let icon = nativeImage.createFromDataURL(icon2x);
  if (!tray) {
    if (process.platform === 'darwin') {
      icon = nativeImage.createFromDataURL(icon1x);
      icon.addRepresentation({
        scaleFactor: 2.0,
        dataURL: icon2x
      });
    }
    tray = new Tray(icon);
  }

  // @ts-ignore
  const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
  tray.setToolTip('TagSpaces');
  tray.setContextMenu(contextMenu);

  // tray.on('click', () => {
  //   tray.popUpContextMenu(contextMenu);
  // })
  // return tray;
}
